import { LOGIN_TOKEN } from '@/global/constances'
// import useLoginStore from '@/store/login/login'
import { localcache } from '@/utils/cache'
import { firstMenu } from '@/utils/map-menus'
// import { LOGIN_TOKEN } from '@/global/constances'
import { createRouter, createWebHashHistory } from 'vue-router'
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/main'
    },
    {
      path: '/login',
      component: () => import('@/views/login/Login.vue')
    },
    {
      path: '/main',
      name: 'main',
      component: () => import('@/views/main/main.vue')
    },
    {
      path: '/:pathMatch(.*)',
      component: () => import('../views/not-found/NotFound.vue')
    }
  ]
})
// 导航守卫
router.beforeEach((to) => {
  const token = localcache.getCache(LOGIN_TOKEN)
  if (to.path === '/main' && !token) {
    return '/login'
  }
  // 当进入main页面。让其进入菜单的第一个页面中
  if (to.path === '/main') {
    return firstMenu?.url
  }
})
export default router
